using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

using FunMazeEngine;


namespace RemotableObjects
{

	public class frmRServer : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
//		private MazeServerObject remotableObject;
        private string errorMessage = string.Empty;
        private bool serverStarted = false;
        private Button button1;
        private TextBox textBox2;
	
		private System.ComponentModel.Container components = null;

        public frmRServer()
        {
            try
            {
                InitializeComponent();
  //              remotableObject = new MazeServerObject();

                TcpChannel channel = new TcpChannel(8080);
                ChannelServices.RegisterChannel(channel, false);
    //            RemotingConfiguration.RegisterWellKnownServiceType(typeof(MazeServerObject), "MazeServer", WellKnownObjectMode.Singleton);
                serverStarted = true;
            }
            catch (RemotingException rex)
            {
                serverStarted = false;
                errorMessage = rex.StackTrace;
            }


            InitializeComponent();
            //			remotableObject = new MazeServerObject();

            //TcpChannel channel = new TcpChannel(8080);
            //ChannelServices.RegisterChannel(channel);
            //			RemotingConfiguration.RegisterWellKnownServiceType(typeof(MazeServerObject),"HelloWorld",WellKnownObjectMode.Singleton);
            //			RemotableObjects.Cache.Attach(this);
        }

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 12);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ReadOnly = true;
            this.textBox1.Size = new System.Drawing.Size(344, 222);
            this.textBox1.TabIndex = 1;
            //this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(462, 52);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 2;
            this.button1.Text = "Test";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(363, 98);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(281, 127);
            this.textBox2.TabIndex = 3;
            // 
            // frmRServer
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(656, 246);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "frmRServer";
            this.Text = "RemoteServer";
            this.Load += new System.EventHandler(this.frmRServer_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion


		[STAThread]
		static void Main() 
		{
			Application.Run(new frmRServer());
		}

		#region IObserver Members

		public void Notify(string text)
		{
			textBox1.Text = text;
		}

		#endregion

//        private void textBox1_TextChanged(object sender, EventArgs e)
 //       {

  //      }

        private void frmRServer_Load(object sender, EventArgs e)
        {
            if (serverStarted)
            {
                textBox1.Text = "Server start successfully";
            }
            else
            {
                textBox1.Text = errorMessage;
            }

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Map map = new Map();
            map.CreateMap(10, 100);
            Dictionary<int, int> treasureList = map.GetTreasure();

            foreach (var item in treasureList)
            {
               textBox2.Text += item.Key + ":" + item.Value + "\r\n";
            }

            textBox2.Text = "Hello";
        }
	}
}
